#include "muduo/protobuf/codec.h"
#include "muduo/protobuf/dispatcher.h"

//这里没有加../include是因为我们-I指定了头文件寻找位置
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include "request.pb.h"

class Server
{
private:
    using MessagePtr = std::shared_ptr<google::protobuf::Message>;
    using TranslateRequestPtr = std::shared_ptr<lkm::TranslateRequest>;
    using AddRequestPtr = std::shared_ptr<lkm::AddRequest>;

    muduo::net::TcpServer _server;
    muduo::net::EventLoop _baseloop;
    ProtobufCodec _codec;           // 协议处理器 对收到的请求进行protobuf协议处理
    ProtobufDispatcher _dispatcher; // 请求分发器
private:
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn,
                          const MessagePtr &message,
                          muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected() == true)
        {
            LOG_INFO << "新连接建立了";
        }
        else
        {
            LOG_INFO << "连接断开了";
        }
    }

    std::string Translate(const std::string &text)
    {
        static std::unordered_map<std::string, std::string> dict{
            {"hello", "你好"},
            {"yes", "是的"},
            {"red", "红色"}};

        auto it = dict.find(text);
        if (it == dict.end())
        {
            return "UnKonw";
        }

        return it->second;
    }

    // 注册进ProtobufDispatcher中的哈希表
    void OnTranslate(const muduo::net::TcpConnectionPtr &conn,
                     const TranslateRequestPtr &message,
                     muduo::Timestamp)
    {
        // 获取客户端发送的内容
        std::string ret = message->msg();
        // 业务处理
        std::string msg = Translate(ret);
        // 组织protobuf的响应
        lkm::TranslateResponse response;
        response.set_msg(msg);
        // 发送响应 这个_codec.send会帮我们把这个response做protobuf序列化
        _codec.send(conn, response);
    }

    void OnAdd(const muduo::net::TcpConnectionPtr &conn,
               const AddRequestPtr &message,
               muduo::Timestamp)
    {
        // 获取客户端发送的内容
        int num1 = message->num1();
        int num2 = message->num2();
        // 业务处理
        int result = num1 + num2;
        // 组织protobuf的响应
        lkm::AddResponse response;
        response.set_result(result);
        // 发送响应 这个_codec.send会帮我们把这个response做protobuf序列化
        _codec.send(conn, response);
    }

public:
    Server(int port) : _server(&_baseloop, muduo::net::InetAddress("0.0.0.0", port), "server", muduo::net::TcpServer::kReusePort),
                       _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
                       _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        //注册请求处理函数
        _dispatcher.registerMessageCallback<lkm::TranslateRequest>(std::bind(&Server::OnTranslate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<lkm::AddRequest>(std::bind(&Server::OnAdd, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }

    void Start()
    {
        _server.start();
        _baseloop.loop();
    }
};

int main()
{
    Server ser(8888);
    ser.Start();

    return 0;
}